package cn.icatw.leetcode.editor.cn;
//给你一棵二叉树的根节点 root ，返回其节点值的 后序遍历 。
//
//
//
// 示例 1：
//
//
//输入：root = [1,null,2,3]
//输出：[3,2,1]
//
//
// 示例 2：
//
//
//输入：root = []
//输出：[]
//
//
// 示例 3：
//
//
//输入：root = [1]
//输出：[1]
//
//
//
//
// 提示：
//
//
// 树中节点的数目在范围 [0, 100] 内
// -100 <= Node.val <= 100
//
//
//
//
// 进阶：递归算法很简单，你可以通过迭代算法完成吗？
//
// Related Topics 栈 树 深度优先搜索 二叉树 👍 1181 👎 0


import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

//Java：二叉树的后序遍历
public class T145_BinaryTreePostorderTraversal {
    public static void main(String[] args) {
        Solution solution = new T145_BinaryTreePostorderTraversal().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            LinkedList<Integer> result = new LinkedList<>();
            //m(root, result);
            Stack<TreeNode> stack = new Stack<>();
            if (root == null) {
                return result;
            }
            stack.push(root);
            //iterativeTraversalMethod(stack, result);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                if (node != null) {
                    stack.push(node);
                    stack.push(null);
                    if (node.right != null) {
                        stack.push(node.right);
                    }
                    if (node.left != null) {
                        stack.push(node.left);
                    }
                } else {
                    node = stack.pop();
                    result.add(node.val);
                }
            }
            return result;
        }

/*        private void iterativeTraversalMethod(Stack<TreeNode> stack, LinkedList<Integer> result) {
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                result.addFirst(node.val);
                if (node.left != null) {
                    stack.push(node.left);
                }
                if (node.right != null) {
                    stack.push(node.right);
                }
            }
        }*/

/*        public void m(TreeNode root, List<Integer> res) {
            if (root == null) {
                return;
            }
            m(root.left, res);
            m(root.right, res);
            res.add(root.val);
        }*/
    }

    //leetcode submit region end(Prohibit modification and deletion)
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
